﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.Model;

namespace HuaCheng.Views
{
    /// <summary>
    /// 查看日志
    /// </summary>
    public partial class CheckLog : Form
    {
        //数据库查询的所有设备日志记录
        DataTable deviceDt;
        //所有本地日志记录
        DataTable localDt;
        //所有跳转日志记录
        DataTable jumpDt;
        //所有故障日志记录
        DataTable errorDt;
        //柜号
        public int GID;
        //中位机号
        public int MID;
        //通道号
        int chl_id;
        public CheckLog(int gid, int mid,int cid)
        {
            InitializeComponent();
            this.GID = gid;
            this.MID = mid;
            this.chl_id = cid;
        }
        /// <summary>
        /// 窗体Load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckLog_Load(object sender, System.EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
            LoadLocalData();
            //激活OnPageChanged事件
            pcPagination.OnPageChanged += new EventHandler(pagerControl_OnPageChanged);
        }

        private void pagerControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            pcPagination.PageIndex = 1;
            if (tcLog.SelectedTab.Name == "tpDeviceLog")
            {
                LoadDeviceData();
            }
            else if (tcLog.SelectedTab.Name == "tpLocalLog")
            {
                LoadLocalData();
            }
            else if (tcLog.SelectedTab.Name == "tpJumpLog")
            {
                LoadJumpData();
            }
            else if (tcLog.SelectedTab.Name == "tpErrorLog")
            {
                LoadErrorData();
            }
        }
        private void pagerControl_OnPageChanged(object sender, EventArgs e)
        {
            if (tcLog.SelectedTab.Name == "tpDeviceLog")
            {
                LoadDeviceData();
            }
            else if (tcLog.SelectedTab.Name == "tpLocalLog")
            {
                LoadLocalData();
            }
            else if (tcLog.SelectedTab.Name == "tpJumpLog")
            {
                LoadJumpData();
            }
            else if (tcLog.SelectedTab.Name == "tpErrorLog")
            {
                LoadErrorData();
            }
        }
        /// <summary>
        /// 加载设备日志记录
        /// </summary>
        /// <param name="unit_id"></param>
        /// <param name="chl_id"></param>
        private void LoadDeviceData()
        {
            List<DeviceLog> deviceList = DeviceLogHelper.RetrieveDeviceLog(IndexPage.devIp, GID, MID,chl_id);
            deviceDt = DataTableHelper.ToDataTable<DeviceLog>(deviceList);
            int count = deviceDt.Rows.Count;
            pcPagination.RecordCount = count;
            LoadPage(pcPagination.PageIndex, pcPagination.PageSize, pcPagination.PageCount, count, deviceDt);
            pcPagination.DrawControl(count);
        }
        /// <summary>
        /// 加载本地日志记录
        /// </summary>
        private void LoadLocalData()
        {
            List<LocalLog> localList=TxtControl.ReadListFromTxt();
            localDt = DataTableHelper.ToDataTable<LocalLog>(localList);
            int count = localDt.Rows.Count;
            pcPagination.RecordCount = count;
            LoadPage(pcPagination.PageIndex, pcPagination.PageSize, pcPagination.PageCount, count, localDt);
            pcPagination.DrawControl(count);
        }
        /// <summary>
        /// 加载跳转日志记录
        /// </summary>
        private void LoadJumpData()
        {
            List<JumpReasonRecord> jumpLog = JumpReasonRecordControl.RetrieveJumpLog(IndexPage.devIp, GID, MID,chl_id);
            jumpDt = DataTableHelper.ToDataTable<JumpReasonRecord>(jumpLog);
            int count = jumpDt.Rows.Count;
            pcPagination.RecordCount = count;
            LoadPage(pcPagination.PageIndex, pcPagination.PageSize, pcPagination.PageCount, count, jumpDt);
            pcPagination.DrawControl(count);
        }
        /// <summary>
        /// 加载故障日志记录
        /// </summary>
        private void LoadErrorData()
        {
            List<Errors> errorLog=ErrorHelper.RetrieveErrorLog(IndexPage.devIp, GID,MID,chl_id);
            errorDt = DataTableHelper.ToDataTable<Errors>(errorLog);
            int count = errorDt.Rows.Count;
            pcPagination.RecordCount = count;
            LoadPage(pcPagination.PageIndex, pcPagination.PageSize, pcPagination.PageCount, count, errorDt);
            pcPagination.DrawControl(count);
        }
        /// <summary>
        /// 加载当前页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="recordCount"></param>
        private void LoadPage(int pageIndex,int pageSize,int pageCount,int recordCount,DataTable dt)
        {
            //当前页码
            if (pageIndex < 1) pageIndex = 1;
            if (pageIndex > pageCount) pageIndex = pageCount;
            //当前页开始的记录号
            int beginRecord;
            //当前页最后一条记录号
            int endRecord;
            //当前页的数据表
            DataTable dtTemp;
            dtTemp = dt.Clone();
            beginRecord = pageSize * (pageIndex - 1);
            if (pageIndex == 1) beginRecord = 0;
            endRecord = pageSize * pageIndex;
            if (pageIndex == pageCount) endRecord = recordCount;
            if (beginRecord < 0)
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Current page doesn't have logs！");
                }
                else
                {
                    MessageBox.Show("当前页没有日志记录！");
                }
            }
            else
            {
                //给当前页赋值
                for (int i = beginRecord; i < endRecord; i++)
                {
                    dtTemp.ImportRow(dt.Rows[i]);
                }
                if (tcLog.SelectedTab.Name == "tpDeviceLog")
                {
                    dgvDevice.DataSource = dtTemp;  //datagridview控件名是dgvDevice
                    //设置显示的列名
                    dgvDevice.Columns["DevIp"].HeaderText = ResourceCulture.GetString("DevID");//设备号
                    dgvDevice.Columns["GID"].HeaderText = ResourceCulture.GetString("Group");//柜号
                    dgvDevice.Columns["ChlId"].HeaderText = ResourceCulture.GetString("ChannelID");//通道号
                    dgvDevice.Columns["TestId"].HeaderText = ResourceCulture.GetString("TestID");//测试号
                    dgvDevice.Columns["OperationType"].HeaderText = ResourceCulture.GetString("OperationType");//操作类型
                    dgvDevice.Columns["TestATime"].HeaderText = ResourceCulture.GetString("OperationTime");//操作时间
                    dgvDevice.Columns["Operator"].HeaderText = ResourceCulture.GetString("Operator");//操作者
                    dgvDevice.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    this.dgvDevice.Columns[1].FillWeight = 200;
                    this.dgvDevice.Columns[4].FillWeight = 300;
                    this.dgvDevice.Columns[5].FillWeight = 300;
                    this.dgvDevice.Columns[6].FillWeight = 300;
                    this.dgvDevice.Columns[7].FillWeight = 300;
                }
                else if (tcLog.SelectedTab.Name == "tpLocalLog")
                {
                    dgvLocal.DataSource = dtTemp;
                    dgvLocal.Columns["TestATime"].HeaderText = ResourceCulture.GetString("TestATime");//测试时间
                    dgvLocal.Columns["Record"].HeaderText = ResourceCulture.GetString("Record");//记录
                    dgvLocal.Columns["LocalEvent"].HeaderText = ResourceCulture.GetString("Event");//事件
                    dgvLocal.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                }
                else if (tcLog.SelectedTab.Name == "tpJumpLog")
                {
                    dgvJump.DataSource = dtTemp;
                    dgvJump.Columns["TestId"].HeaderText = ResourceCulture.GetString("TestID");//测试号
                    dgvJump.Columns["DevIp"].HeaderText = ResourceCulture.GetString("DevID");//设备号
                    dgvJump.Columns["GID"].HeaderText = ResourceCulture.GetString("Group");//柜号
                    dgvJump.Columns["ChlId"].HeaderText = ResourceCulture.GetString("ChannelID");//通道号
                    dgvJump.Columns["StepIndex"].HeaderText = ResourceCulture.GetString("StepID");//工步号
                    dgvJump.Columns["Circ"].HeaderText = ResourceCulture.GetString("CycleID");//循环号
                    dgvJump.Columns["JumpTime"].HeaderText = ResourceCulture.GetString("JumpTime");//跳转时间
                    dgvJump.Columns["JumpReason"].HeaderText = ResourceCulture.GetString("JumpReason");//跳转原因
                    dgvJump.Columns["JumpResult"].HeaderText = ResourceCulture.GetString("JumpResult");//跳转结果
                    dgvJump.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    this.dgvJump.Columns[1].FillWeight = 200;
                    this.dgvJump.Columns[2].FillWeight = 200;
                    this.dgvJump.Columns[7].FillWeight = 300;
                    this.dgvJump.Columns[8].FillWeight = 200;
                    this.dgvJump.Columns[9].FillWeight = 300;
                }
                else if (tcLog.SelectedTab.Name == "tpErrorLog")
                {
                    dgvError.DataSource = dtTemp;
                    dgvError.Columns["IP"].HeaderText = ResourceCulture.GetString("DevID");//设备号
                    dgvError.Columns["GID"].HeaderText = ResourceCulture.GetString("Group");//柜号
                    dgvError.Columns["CID"].HeaderText = ResourceCulture.GetString("ChannelID");//通道号
                    dgvError.Columns["OccurTime"].HeaderText = ResourceCulture.GetString("OccurTime");//发生时间
                    dgvError.Columns["ErrorCause"].HeaderText = ResourceCulture.GetString("ErrorCause");//故障原因
                    dgvError.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    this.dgvError.Columns[4].FillWeight = 300;
                    this.dgvError.Columns[5].FillWeight = 700;
                }
            }
        }
        /// <summary>
        /// 序号列赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDevice_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvDevice.Columns[e.ColumnIndex].Name == "deviceId")//最左边添加一列ID 即可
            {
                e.Value = e.RowIndex + 1;
            }
        }
        private void dgvLocal_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvLocal.Columns[e.ColumnIndex].Name == "localId")//最左边添加一列ID 即可
            {
                e.Value = e.RowIndex + 1;
            }
        }
        private void dgvJump_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvJump.Columns[e.ColumnIndex].Name == "jumpId")//最左边添加一列ID 即可
            {
                e.Value = e.RowIndex + 1;
            }
        }
        private void dgvError_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dgvError.Columns[e.ColumnIndex].Name == "errorId")//最左边添加一列ID 即可
            {
                e.Value = e.RowIndex + 1;
            }
        }
        #region 国际化
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("Log");
            this.localId.HeaderText = ResourceCulture.GetString("Index");
            this.deviceId.HeaderText = ResourceCulture.GetString("Index");
            this.errorId.HeaderText = ResourceCulture.GetString("Index");
            this.jumpId.HeaderText = ResourceCulture.GetString("Index");
            this.tpDeviceLog.Text = ResourceCulture.GetString("DeviceLog");
            this.tpErrorLog.Text = ResourceCulture.GetString("ErrorLog");
            this.tpJumpLog.Text = ResourceCulture.GetString("JumpLog");
            this.tpLocalLog.Text = ResourceCulture.GetString("LocalLog");
        }
        #endregion

        private void pcPagination_Load(object sender, EventArgs e)
        {
            pcPagination.SetResourceCulture();
        }
    }
}
